package cn.stylefeng.guns.sys.core.auth.filter;

import cn.stylefeng.guns.sys.core.util.RedisUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 接口过滤器
 *
 * @author zwf
 * @Date 20210819
 */
@Service
public class InterfaceSecurityFilter implements Filter {

    @Resource
    private RedisUtils redisUtils;
    @Override
    public void init(FilterConfig filterConfig)throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {
        servletResponse.setCharacterEncoding("UTF8");
        HttpServletRequest request =   (HttpServletRequest)servletRequest;
        HttpServletResponse response =   (HttpServletResponse)servletResponse;

        String client = request.getHeader("client");
        if(client==null){
            client = request.getParameter("client");
        }
        if(client==null||client.equals("")){
            PrintWriter out = response.getWriter();
            JSONObject r = new JSONObject();
            r.put("success", false);
            r.put("msg", "参数[client]未空！");
            r.put("errorcode","404");
            out.print(r.toJSONString());
        }
        System.out.println(request.getRequestURL());
        String token = request.getHeader("token");
        if(token==null){
            token = request.getParameter("token");
        }

        System.out.println("client="+client);
        System.out.println("token="+token);

        if(token!=null){
            //开始进行校验
            boolean isOk = false;
            if(client.equals("android")){
                Object userInfo = redisUtils.hgetOfAndroid(token);
                System.out.println("userInfo="+userInfo);
                request.setAttribute("userInfor",userInfo);
                isOk = true;
            }else if(client.equals("wechat")){
                Object userInfo = redisUtils.hget("tokenMap",token);
                System.out.println("userInfo="+userInfo);
                request.setAttribute("userInfor",userInfo);
                isOk = true;
            }else {
                isOk = true;
            }
            if(!isOk){
                servletResponse.setCharacterEncoding("UTF8");
                PrintWriter out = response.getWriter();
                JSONObject r = new JSONObject();
                r.put("success", false);
                r.put("msg", "请求未通过校验");
                r.put("errorcode","404");
                out.print(r.toJSONString());
            }else {
                long start = System.currentTimeMillis();
                filterChain.doFilter(servletRequest,servletResponse);
                System.out.println("Execute zwf cost="+(System.currentTimeMillis()-start));
            }
        }else {
            long start = System.currentTimeMillis();
            filterChain.doFilter(servletRequest,servletResponse);
            System.out.println("Execute zwf cost="+(System.currentTimeMillis()-start));
        }
    }

    @Override
    public void destroy() {

    }

}
